import { createStore } from 'vuex'
import { fetchRoleList } from '@/api/role'
import { fetchUser } from '@/api/user'

export default createStore({
  state: {
    roleList: [], // 所有角色
    user: {}, // 用户信息
    role: {}, // 当前用户的角色
  },
  mutations: {
    setRoleList (state, roleList) {
      state.roleList = roleList
    },
    setUser (state, user) {
      state.user = user
    },
    setRole (state, role) {
      state.role = role
    },
  },
  actions: {
    async getRoleList (store) {
      await fetchRoleList().then(({ data }) => {
          store.commit('setRoleList', data)
        },
      )
    },
    async getUser (store) {
      await fetchUser().then(res => {
        store.commit('setUser', res.data)
      })
    },
  },
  modules: {},
})
